package the.workshop.milktea.interfaces.api;

import io.swagger.annotations.Api;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import the.workshop.milktea.infrastructure.model.IngredientCollect;
import the.workshop.milktea.interfaces.resp.IngredientPageResp;
import the.workshop.milktea.service.IngredientCollectService;
import the.workshop.milktea.service.IngredientService;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "Tea")
@RestController
@RequestMapping("/api/tea/ingredient")
public class ApiIngredientController {

    @Resource
    private IngredientService ingredientService;

    @Resource
    private IngredientCollectService ingredientCollectService;

    @GetMapping("/list")
    public APIResult<List<IngredientPageResp>> list() {
        return APIResult.success(ingredientService.getList());
    }

    @PostMapping("/collect")
    public APIResult<Boolean> collect(@RequestBody List<IngredientCollect> data) {
        if (CollectionUtils.isEmpty(data)) {
            return APIResult.success(false);
        }

        for (IngredientCollect item : data) {
            ingredientCollectService.add(item);
        }
        return APIResult.success(true);
    }
}
